package com.muyu.danmo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoTrainingInformation;
import com.muyu.danmo.service.IDanmoTrainingInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


/**
 * 培训班详情Controller
 *
 * @author yangle
 * @date 2024-10-10
 */
@RestController
@RequestMapping("/trainingInformation")
public class DanmoTrainingInformationController extends BaseController {
    @Autowired
    private IDanmoTrainingInformationService danmoTrainingInformationService;

    /**
     * 查询培训班详情列表
     */
//    @RequiresPermissions("system:information:list")
    @PostMapping("/list")
    public Result list(@RequestBody DanmoTrainingInformation danmoTrainingInformation) {
        PageHelper.startPage(danmoTrainingInformation.getPageNum(), danmoTrainingInformation.getPageSize());
        List<DanmoTrainingInformation> list;
        if (danmoTrainingInformation.getSort() == 0) {
            list = danmoTrainingInformationService.selectDanmoTrainingInformationList(danmoTrainingInformation);
        } else if (danmoTrainingInformation.getSort() == 1) {
            list = danmoTrainingInformationService.selectDanmoTrainingInformationViewsList(danmoTrainingInformation);
        } else {
            list = danmoTrainingInformationService.selectDanmoTrainingInformationAmountPraiseList(danmoTrainingInformation);
        }
        PageInfo<DanmoTrainingInformation> danmoInformationPageInfo = new PageInfo<>(list);
        return Result.success(danmoInformationPageInfo);
    }

    /**
     * 查询培训班详情列表(按浏览量排序)
     */
//    @RequiresPermissions("system:information:list")
    @PostMapping("/hot/views")
    public Result views(@RequestBody DanmoTrainingInformation danmoTrainingInformation) {
        PageHelper.startPage(danmoTrainingInformation.getPageNum(), danmoTrainingInformation.getPageSize());
        List<DanmoTrainingInformation> list = danmoTrainingInformationService.selectDanmoTrainingInformationViewsList(danmoTrainingInformation);
        PageInfo<DanmoTrainingInformation> danmoInformationPageInfo = new PageInfo<>(list);
        return Result.success(danmoInformationPageInfo);
    }

    /**
     * 查询培训班详情列表(按点赞量排序)
     */
//    @RequiresPermissions("system:information:list")
    @PostMapping("/hot/amountPraise")
    public Result amountPraise(@RequestBody DanmoTrainingInformation danmoTrainingInformation) {
        PageHelper.startPage(danmoTrainingInformation.getPageNum(), danmoTrainingInformation.getPageSize());
        List<DanmoTrainingInformation> list = danmoTrainingInformationService.selectDanmoTrainingInformationAmountPraiseList(danmoTrainingInformation);
        PageInfo<DanmoTrainingInformation> danmoInformationPageInfo = new PageInfo<>(list);
        return Result.success(danmoInformationPageInfo);
    }


    /**
     * 导出培训班详情列表
     */
//    @RequiresPermissions("system:information:export")
    @Log(title = "培训班详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DanmoTrainingInformation danmoTrainingInformation) {
        List<DanmoTrainingInformation> list = danmoTrainingInformationService.selectDanmoTrainingInformationList(danmoTrainingInformation);
        ExcelUtil<DanmoTrainingInformation> util = new ExcelUtil<DanmoTrainingInformation>(DanmoTrainingInformation.class);
        util.exportExcel(response, list, "培训班详情数据");
    }

    /**
     * 获取培训班详情详细信息
     */
//    @RequiresPermissions("system:information:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser==null){
            return Result.notLoggedIn("请登录");
        }
        DanmoTrainingInformation danmoTrainingInformation = danmoTrainingInformationService.selectDanmoTrainingInformationById(id);
        if (danmoTrainingInformation == null) {
            return Result.error("没有查询到数据");
        }
        return Result.success(danmoTrainingInformation);
    }

    /**
     * 新增培训班详情
     */
//    @RequiresPermissions("system:information:add")
    @Log(title = "培训班详情", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result add(@RequestBody DanmoTrainingInformation danmoTrainingInformation) {

        return danmoTrainingInformationService.insertDanmoTrainingInformation(danmoTrainingInformation);
    }

    /**
     * 修改培训班详情
     */
//    @RequiresPermissions("system:information:edit")
    @Log(title = "培训班详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody DanmoTrainingInformation danmoTrainingInformation) {
        return danmoTrainingInformationService.updateDanmoTrainingInformation(danmoTrainingInformation);
    }

    /**
     * 删除培训班详情
     */
//    @RequiresPermissions("system:information:remove")
    @Log(title = "培训班详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids) {
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        if (loginUser==null){
//            return error("请登录");
//        }
        return danmoTrainingInformationService.deleteDanmoTrainingInformationByIds(ids);
    }


    @GetMapping("/getDanmoTrainingInformationById/{id}")
    public Result getDanmoTrainingInformationById(@PathVariable("id") Long id) {
        return Result.success(danmoTrainingInformationService.selectDanmoTrainingInformationById(id));
    }

//    /**
//     * 添加培训班信息
//     *
//     * @param danmoTrainingInformation
//     * @return
//     */
//    @PostMapping("addDanmoTrainingInformation")
//    public Result addDanmoTrainingInformation(@RequestBody DanmoTrainingInformation danmoTrainingInformation) {
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        if (loginUser == null) {
//            return Result.notLoggedIn("请登录");
//        }
//        return danmoTrainingInformationService.addDanmoTrainingInformation(danmoTrainingInformation);
//    }


//    /**
//     * 、修改
//     * @param danmoTrainingInformation
//     * @return
//     */
//    @PutMapping
//    public Result editDanmoTrainingInformation(@RequestBody DanmoTrainingInformation danmoTrainingInformation)
//    {
//        danmoTrainingInformationService.updateDanmoTrainingInformation(danmoTrainingInformation);
//        return Result.success("修改成功");
//    }

    /**
     * 查看自己详情
     */
    @GetMapping("/DanmoTrainingInformationByUserId")
    public Result getDanmoTrainingInformationByUserId() {
        DanmoTrainingInformation danmoTrainingInformation = new DanmoTrainingInformation();
        PageHelper.startPage(danmoTrainingInformation.getPageNum(), danmoTrainingInformation.getPageSize());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return error("请登录");
        }
        Long userId = loginUser.getUserid();
//        Long userId = Long.valueOf(1); // 固定的用户ID
        List<DanmoTrainingInformation> danmoTrainingInformations = danmoTrainingInformationService.selectDanmoTrainingInformationByUserId(userId);

        PageInfo<DanmoTrainingInformation> danmoInformationPageInfo = new PageInfo<>(danmoTrainingInformations);

        return Result.success(danmoInformationPageInfo);
    }


//    /**
//     * 修改培训班信息
//     *
//     * @param danmoTrainingInformation 培训信息
//     * @return 结果对象
//     */
//    @PutMapping("updateDanmoTrainingInformation")
//    public Result updateDanmoTrainingInformation(@RequestBody DanmoTrainingInformation danmoTrainingInformation) {
//        // 获取当前用户信息
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        if (loginUser == null) {
//            return Result.notLoggedIn("请登录");
//        }
//
//        // 设置用户 ID（如果需要）
//        Long userId = loginUser.getUserid();
//        danmoTrainingInformation.setUserId(userId);
//
//        // 调用服务层处理修改逻辑
//        return Result.success(danmoTrainingInformationService.updateDanmoTrainingInformation(danmoTrainingInformation));
//    }
}
